package app.purchase.a571xz.com.myandroidframe.httpservice.base;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.TreeMap;

import app.purchase.a571xz.com.myandroidframe.utils.MD5util;

/**
 * Created by Administrator on 2017/12/27.
 */

public class RequestBean {

    private final RequestBean bc = setBC();

    private RequestBean setBC() {
        return this;
    }

    /**
     * 待定 能获取到子类字段，但无法为字段指定值，键与值无法一一对应
     */
    public void getAll() {
        //this指向子类
        Class clazz = this.bc.getClass();
        TreeMap<String, String> signatureSortMap = new TreeMap<>();
        Field[] fields = clazz.getDeclaredFields();
        for (Field f : fields) {       //不知为何每次获取子类字段都会多出""
            if (!f.getName().equals("$change") && !f.getName().equals("serialVersionUID")) {

            }
        }
    }

    public String setSignMap(TreeMap<String, String> map) {
        String signString = "";
        for (Map.Entry entry : map.entrySet()) {

//            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
//            signString = signString + entry.getKey().toString() + entry.getValue().toString().replace(" ","+");
            signString = signString + entry.getKey().toString() + entry.getValue().toString();
        }
//        LogUtils.e("排序后的map:" + signString);
        signString = MD5util.md5Password(signString);
//        LogUtils.e("排序后的map的加密值" + signString);
        return signString;
    }

    /**
     * 字符串转化成为16进制字符串
     *
     * @param s
     * @return
     */
    public static String strTo16(String s) {
        char[] chars = "0123456789abcdef".toCharArray();
        StringBuilder sb = new StringBuilder("");
        byte[] bs = new byte[0];
        try {
            bs = s.getBytes("GB2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        int bit;
        for (int i = 0; i < bs.length; i++) {
            bit = (bs[i] & 0x0f0) >> 4;
            sb.append(chars[bit]);
            bit = bs[i] & 0x0f;
            sb.append(chars[bit]);
            // sb.append(' ');
        }
        return sb.toString().trim();
    }
}
